//验证工具类
const lodash = require('lodash');
const validator = {
    //验证必填项
    async required(column, obj) {
        let objRs = Object.prototype.toString.call(obj) === '[object Object]';
        if (!objRs) {
            throw new Error("验证数据不是Object类型");
        }
        for (var i = 0; i < column.length; i++) {
            let vdt = await validator.isEmpty(obj[column[i]]);
            console.log(`验证的值(${column[i]})===${obj[column[i]]}===${vdt}`);
            if (vdt) {
                //取不到值，表示没有填写
                throw new Error(`缺少${column[i]}参数`);
            }
        }
        return true;
    },
    //取值,过滤多余的参数
    async getSubmitData(column, obj) {
        let retData = {};
        for (var i = 0; i < column.length; i++) {
            retData[column[i]] = obj[column[i]];
        }
        return retData;
    },
    //取值,过滤多余的参数
    async isEmpty(value) {
        let ret = false;
        if (value == null || value == undefined || value == NaN || value == "") {
            ret = true;
        }
        if (lodash.isArrayLikeObject(value)) {
            ret = lodash.isEmpty(value);
        }
        console.log(`${value}=====>isEmpty==`, ret)
        return ret;
    }
}

module.exports = validator;